<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>36. 使用exec完成全局匹配</title>
</head>

<body>
    <h1>houdunren.com</h1>
    <h2>hdcms.com</h2>
    <h1>后盾人</h1>
</body>
<script>
    /**
     * exec 如果不连续调用 只能匹配到第一次，除非连续调用 能匹配到所有
     * lastIndex 下次开始匹配的下标
     **/
    // let hd="houdunren";
    // let reg=/u/gi
    // console.log(reg.exec(hd)); //["u", index: 2, input: "houdunren", groups: undefined]
    // console.log(reg.lastIndex); //3
    // console.log(reg.exec(hd)) //["u", index: 4, input: "houdunren", groups: undefined]
    // console.log(reg.lastIndex);//5
    // console.log(reg.exec(hd))  //null
    // console.log(reg.lastIndex); //0
    // let result=[];
    // while(res=reg.exec(hd)){
    //     console.log(res)
    //     result.push(res);
    // }
    // console.log(result); //(2) [Array(1), Array(1)]

    function search(string, reg) {
        let result = [];
        while ((res = reg.exec(string))) {
            result.push(res);
        }

        return result;
    }

    let matches= search(document.body.innerHTML,/<(h[1-6])>([\s\S]+?)<\/\1>/ig)
    console.log(matches);
</script>

</html>